package com.zc.zby.basicframedemo.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.zc.zby.basicframedemo.R;
import com.zc.zby.basicframedemo.base.BaseActivity;
import com.zc.zby.basicframedemo.utils.ToolUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;

public class ConstraintLayoutActivity extends BaseActivity {

    @BindView(R.id.iv_add)
    protected ImageView mIvAdd;
    @BindView(R.id.iv_cz)
    protected ImageView mIvCz;
    @BindView(R.id.iv_qc)
    protected ImageView mIvQc;
    @BindView(R.id.iv_jg)
    protected ImageView mIvJg;
    @BindView(R.id.iv_dq)
    protected ImageView mIvDq;
    @BindView(R.id.iv_dq_jg)
    protected ImageView mIvDqJg;
    @BindView(R.id.iv_dq_dw)
    protected ImageView mIvDqDw;
    // 动画集合，用来控制动画的有序播放
    private AnimatorSet animatorSet;
    // 圆的半径
    private int radius;
    // FloatingActionButton宽度和高度，宽高一样
    private int width;

    @Override
    public int getLayoutId() {
        return R.layout.activity_constraint_layout;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        // 将三个弹出的FloatingActionButton隐藏
        setViewVisible(false);
    }

    private void initDq() {
        ValueAnimator earthAnimator = animatePlanet(mIvDqJg, TimeUnit.SECONDS.toMillis(3));
        ValueAnimator marsAnimator = animatePlanet(mIvDqDw, TimeUnit.SECONDS.toMillis(4));
        earthAnimator.start();
        marsAnimator.start();
    }

    private ValueAnimator animatePlanet(ImageView planet, long orbitDuration) {
        ValueAnimator anim = ValueAnimator.ofInt(0, 359);
        anim.addUpdateListener(valueAnimator -> {
            int val = (Integer) valueAnimator.getAnimatedValue();
            ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) planet.getLayoutParams();
            layoutParams.circleAngle = val;
            planet.setLayoutParams(layoutParams);
        });
        anim.setDuration(orbitDuration);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatMode(ValueAnimator.RESTART);
        anim.setRepeatCount(ValueAnimator.INFINITE);
        return anim;
    }

    private void setViewVisible(boolean isShow) {
        mIvCz.setVisibility(isShow ? View.VISIBLE : View.GONE);
        mIvQc.setVisibility(isShow ? View.VISIBLE : View.GONE);
        mIvJg.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setListeners() {
        mIvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 播放动画的时候不可以点击
                if (animatorSet != null && animatorSet.isRunning())
                    return;
                // 判断播放显示还是隐藏动画
                if (mIvQc.getVisibility() != View.VISIBLE) {
                    animatorSet = new AnimatorSet();
                    ValueAnimator likeAnimator = getValueAnimator(mIvQc, false, 0);
                    ValueAnimator writeAnimator = getValueAnimator(mIvJg, false, 45);
                    ValueAnimator topAnimator = getValueAnimator(mIvCz, false, 90);
                    animatorSet.playSequentially(likeAnimator, writeAnimator, topAnimator);
                    animatorSet.start();
                } else {
                    animatorSet = new AnimatorSet();
                    ValueAnimator likeAnimator = getValueAnimator(mIvQc, true, 0);
                    ValueAnimator writeAnimator = getValueAnimator(mIvJg, true, 45);
                    ValueAnimator topAnimator = getValueAnimator(mIvCz, true, 90);
                    animatorSet.playSequentially(topAnimator, writeAnimator, likeAnimator);
                    animatorSet.start();
                }

            }
        });
        mIvDq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 初始化地图旋转
                initDq();
            }
        });
    }

    @Override
    public void initToolBar() {
        setToolBarTitle("ConstraintLayout");
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        // 动态获取FloatingActionButton的宽
        mIvAdd.post(new Runnable() {
            @Override
            public void run() {
                width = mIvAdd.getMeasuredWidth();
            }
        });
        // 在xml文件里设置的半径
        radius = ToolUtil.dip2px(this, 80);
    }

    /**
     * 获取ValueAnimator
     *
     * @param button  FloatingActionButton
     * @param reverse 开始还是隐藏
     * @param angle   angle 转动的角度
     * @return ValueAnimator
     */
    private ValueAnimator getValueAnimator(final ImageView button, final boolean reverse, final int angle) {
        ValueAnimator animator;
        if (reverse)
            animator = ValueAnimator.ofFloat(1, 0);
        else
            animator = ValueAnimator.ofFloat(0, 1);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float v = (float) animation.getAnimatedValue();
                ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) button.getLayoutParams();
                params.circleRadius = (int) (radius * v);
                params.width = (int) (width * v);
                params.height = (int) (width * v);
                button.setLayoutParams(params);
            }
        });
        animator.addListener(new SimpleAnimation() {
            @Override
            public void onAnimationStart(Animator animation) {
                button.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (button == mIvQc && reverse) {
                    setViewVisible(false);
                }
            }
        });
        animator.setDuration(200);
        animator.setInterpolator(new DecelerateInterpolator());
        return animator;
    }

    abstract class SimpleAnimation implements Animator.AnimatorListener {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        animatorSet.cancel();
    }
}
